import { request } from '../utils/request';

export default {

  namespace: 'classsortmodel',

  state: {
    currentid: 0, //当前获取的记录数
    getnum: 5, //每次获取的记录数
    classlist: [],
    hasmore: true
  },

  reducers: {
    save(state, { payload: { data } }) {
      let hasmore = true;
      if (data.length === 0) hasmore = false;
      const classlist = [...state.classlist, ...data];
      const currentid = classlist.length;
      return { ...state, hasmore, classlist, currentid };
    },
    savechange(state, { payload: { data } }) {
      let hasmore = true;
      if (data.length < 5) hasmore = false;
      const classlist = [...data];
      const currentid = classlist.length;
      return { ...state, hasmore, classlist, currentid };
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      dispatch({ type: 'getinfo', payload: { sorttext1: '全部', sorttext2: '全部' } })
    },
  },

  effects: {
    *getinfo({ payload }, { put, select }) {
      // request params
      let getnum = yield select(state => state.classsortmodel.getnum)
      let currentid = yield select(state => state.classsortmodel.currentid)
      // get data
      const param = `getnum=${getnum}&currentid=${currentid}&sorttext1=${payload.sorttext1}&sorttext2=${payload.sorttext2}`;
      let { data } = yield request("http://127.0.0.1:7001/api/v1/classsort?" + param, {
        method: 'get'
      });
      // put data
      yield put({ type: 'save', payload: { data } });
    },
    *getinfochange({ payload }, { put, select }) {
      // request params
      let getnum = 5
      let currentid = 0
      const param = `getnum=${getnum}&currentid=${currentid}&sorttext1=${payload.sorttext1}&sorttext2=${payload.sorttext2}`;
      // get data
      let { data } = yield request("http://127.0.0.1:7001/api/v1/classsort?" + param, {
        method: 'get'
      });
      //put data
      yield put({ type: 'savechange', payload: { data } });
    }
  },
};
